﻿using System;
using fastCSharp;
using System.Collections.Generic;
using fastCSharp.threading;
using System.Threading;

namespace diantou.web
{
    /// <summary>
    /// 第三方登录
    /// </summary>
    [fastCSharp.code.cSharp.webCall(IsPool = true)]
    partial class login : fastCSharp.code.cSharp.webCall.call<login>
    {
        /// <summary>
        /// 人人网用户登录
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        [fastCSharp.code.cSharp.webCall]
        public void Renren(string code, string state)
        {
            diantou.dataAccess.user user = null;
            try
            {
                if (fastCSharp.openApi.state.Verify(state))
                {
                    fastCSharp.openApi.renren.api api = fastCSharp.openApi.renren.config.Default.GetApi(code) ?? fastCSharp.openApi.renren.config.Default.GetApi(code);
                    if (api != null)
                    {
                        fastCSharp.openApi.renren.tokenUser tokenUser = api.User;
                        string email = diantou.dataModel.user.registerWebsiteType.renren.ToString() + diantou.dataAccess.user.WebsiteSplit + tokenUser.id.toString();
                        user = diantou.dataAccess.clientCache.User[diantou.dataAccess.tcpCall.user.getWebsiteUserId(email)];
                        if (user == null)
                        {
                            fastCSharp.openApi.renren.avatar avatar;
                            tokenUser.avatar.min(icon => (int)icon.type, out avatar);
                            diantou.dataAccess.user newUser = new diantou.dataAccess.user
                            {
                                Email = email,
                                Name = tokenUser.name.length() != 0 ? tokenUser.name : fastCSharp.Enum<diantou.dataModel.user.registerWebsiteType, diantou.dataModel.user.registerWebsite>.Array(diantou.dataModel.user.registerWebsiteType.renren).DefaultUserName,
                                Icon = avatar.url ?? string.Empty,
                                LoginToken = fastCSharp.emit.jsonSerializer.ToJson(api.RefreshToken)
                            };
                            user = diantou.dataAccess.tcpCall.user.websiteRegister(newUser);
                        }
                        else if (!user.IsLock) diantou.dataAccess.tcpCall.user.reworkLoginToken(user.Id, fastCSharp.emit.jsonSerializer.ToJson(api.RefreshToken), null);
                        setSession(user);
                    }
                }
            }
            finally { location(user); }
        }
        /// <summary>
        /// QQ用户登录
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        [fastCSharp.code.cSharp.webCall]
        public void QQ(string code, string state)
        {
            diantou.dataAccess.user user = null;
            try
            {
                if (fastCSharp.openApi.state.Verify(state))
                {
                    fastCSharp.openApi.qq.api api = fastCSharp.openApi.qq.config.Default.GetApi(code) ?? fastCSharp.openApi.qq.config.Default.GetApi(code);
                    if (api != null)
                    {
                        string email = diantou.dataModel.user.registerWebsiteType.qq.ToString() + diantou.dataAccess.user.WebsiteSplit + api.OpenId;
                        user = diantou.dataAccess.clientCache.User[diantou.dataAccess.tcpCall.user.getWebsiteUserId(email)];
                        if (user == null)
                        {
                            fastCSharp.openApi.qq.userInfo userInfo = api.GetUserInfo();
                            if (userInfo != null)
                            {
                                diantou.dataAccess.user newUser = new diantou.dataAccess.user
                                {
                                    Email = email,
                                    Name = userInfo.nickname.length() != 0 ? userInfo.nickname : fastCSharp.Enum<diantou.dataModel.user.registerWebsiteType, diantou.dataModel.user.registerWebsite>.Array(diantou.dataModel.user.registerWebsiteType.qq).DefaultUserName,
                                    Icon = userInfo.figureurl_2 ?? string.Empty,//.Replace(@"\/", @"/")
                                    Sex = userInfo.gender != null ? (userInfo.gender == "男" ? diantou.dataModel.user.sex.Male : diantou.dataModel.user.sex.Female) : diantou.dataModel.user.sex.Unknown,
                                    LoginToken = fastCSharp.emit.jsonSerializer.ToJson(api.TokenOpenId)
                                };
                                user = diantou.dataAccess.tcpCall.user.websiteRegister(newUser);
                            }
                        }
                        else if (!user.IsLock) diantou.dataAccess.tcpCall.user.reworkLoginToken(user.Id, fastCSharp.emit.jsonSerializer.ToJson(api.TokenOpenId), null);
                        setSession(user);
                    }
                }
            }
            finally { location(user); }
        }
        /// <summary>
        /// 微博用户登录
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        [fastCSharp.code.cSharp.webCall]
        public void Weibo(string code, string state)
        {
            diantou.dataAccess.user user = null;
            try
            {
                if (fastCSharp.openApi.state.Verify(state))
                {
                    fastCSharp.openApi.weibo.api api = fastCSharp.openApi.weibo.config.Default.GetApi(code) ?? fastCSharp.openApi.weibo.config.Default.GetApi(code);
                    if (api != null)
                    {
                        string email = diantou.dataModel.user.registerWebsiteType.weibo.ToString() + diantou.dataAccess.user.WebsiteSplit + api.Uid;
                        user = diantou.dataAccess.clientCache.User[diantou.dataAccess.tcpCall.user.getWebsiteUserId(email)];
                        if (user == null)
                        {
                            fastCSharp.openApi.weibo.user userInfo = api.GetUser();
                            if (userInfo != null)
                            {
                                diantou.dataAccess.user newUser = new diantou.dataAccess.user
                                {
                                    Email = email,
                                    Name = userInfo.screen_name.length() != 0 ? userInfo.screen_name : fastCSharp.Enum<diantou.dataModel.user.registerWebsiteType, diantou.dataModel.user.registerWebsite>.Array(diantou.dataModel.user.registerWebsiteType.weibo).DefaultUserName,
                                    Remark = userInfo.description ?? string.Empty,
                                    Icon = userInfo.avatar_large ?? userInfo.profile_image_url ?? string.Empty,
                                    Sex = userInfo.gender == "m" ? diantou.dataModel.user.sex.Male : (userInfo.gender == "f" ? diantou.dataModel.user.sex.Female : diantou.dataModel.user.sex.Unknown),
                                    LoginToken = fastCSharp.emit.jsonSerializer.ToJson(api.TokenUid)
                                };
                                user = diantou.dataAccess.tcpCall.user.websiteRegister(newUser);
                            }
                        }
                        else if (!user.IsLock) diantou.dataAccess.tcpCall.user.reworkLoginToken(user.Id, fastCSharp.emit.jsonSerializer.ToJson(api.TokenUid), null);
                        setSession(user);
                    }
                }
            }
            finally { location(user); }
        }
        /// <summary>
        /// 设置Session
        /// </summary>
        /// <param name="user"></param>
        private void setSession(diantou.dataAccess.user user)
        {
            if (user != null && !user.IsLock)
            {
                createResponse();
                SetSession<webConfig.session>(new webConfig.session { Id = user.Id });
                diantou.dataAccess.tcpCall.user.online(user.Id, null);
            }
        }
        /// <summary>
        /// 重定向
        /// </summary>
        /// <param name="user"></param>
        private void location(diantou.dataAccess.user user)
        {
            location(user != null ? (IsCookie(fastCSharp.openApi.state.OpenLoginUrlCookieName) ? diantou.dataModel.path.pub.OpenLogin : user.Path.Setting) : diantou.dataModel.path.pub.LoginError);
        }
    }
}
